from django.shortcuts import render, HttpResponse, redirect


# Create your views here.


def index(request):
    return HttpResponse("欢迎使用")

def user_list(request):
    return render(request, "user_list.html")

def user_add(request):
    name = "张三"
    name2 = ["张三","李四","王五"]
    user_info = {"name1":"zhangsan", "name2":"lisi"}
    return render(request, "user_add.html", {"n1": name, "n2": name2, "n3": user_info})

def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    #username = request.POST["username"]
    #如果为post请求，打印通过表单提交的用户数据
    print(request.POST)
    username = request.POST.get("username")
    password = request.POST.get("password")
    if username == "root" and password == "123":
        #return HttpResponse("登录成功！！！")
        return redirect("https://www.eastmoney.com")

    return render(request, "login.html", {'error_msg' : '用户登录密码错误'})



from app01.models import UserInfo, Department

def orm(request):
    # 测试ORM操作表中的数据
    Department.objects.create(deptname="销售部1")
    Department.objects.create(deptname="运营部门1")
    Department.objects.create(deptname="科技部1")

    UserInfo.objects.create(usernm="张飞", passwd="123", age=20)
    return HttpResponse("成功")


